﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PongXNA
{
    class Ball
    {
        private Rectangle _hitbox; // we need to have a hitbox for testing if an object touch them
        public Rectangle HitBox
        {
            get { return _hitbox; }
            set { _hitbox = value; }
        }

        private Vector2 _vectorposition;// we need to have a position for the Racket
        public Vector2 VectorPosition
        {
            get { return _vectorposition; }
            set { _vectorposition = value; }
        }

        private Vector2 _vecteurvitesse;
        public Vector2 VecteurVitesse
        {
            get { return _vecteurvitesse; }
            set { _vecteurvitesse = value; }
        }

        private Texture2D _spriteball;
        public Texture2D SpriteBall
        {
            get { return _spriteball; }
            set { _spriteball = value; }
        }

        private int _vitesse;
        public int Vitesse
        {
            get { return _vitesse; }
            set { _vitesse = value; }
        }
     
        public static float positionArrivee (Vector2 position, Vector2 direction, double hauteur, double largeur, double BallAndRacketWidth )
        {//CAUTION: X direction not invert! direction;X<0!!
            Vector2 newPosition;
            Vector2 newDirection;
            double x = position.X;
            while ((direction.Y / direction.X) * x + position.Y - position.X * direction.Y / direction.X <= hauteur && (direction.Y / direction.X) * x + position.Y - position.X * direction.Y / direction.X >= 0)
            {
                if (x >= largeur - BallAndRacketWidth)//hauteur ball a recup
                {
                    return (float)((direction.Y / direction.X) * x + position.Y - position.X * direction.Y / direction.X);
                }
                x++;
            }
            newPosition.X = (float)x;
            if ((direction.Y / direction.X) * x + position.Y - position.X * direction.Y / direction.X >= hauteur)
            {
                newPosition.Y = (direction.Y / direction.X) * (float)x + position.Y - position.X * direction.Y / direction.X;
            }
            else
            {
                newPosition.Y = 0;
            }
                newDirection.Y = -direction.Y;
                newDirection.X = direction.X;
                newPosition = newPosition + newDirection;
                return positionArrivee(newPosition, newDirection, hauteur, largeur, BallAndRacketWidth);
            } 
        }
    }
